﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using System.ComponentModel;
using CCH.Entity;

namespace CCH.DAO
{
    [DataObject(true)]
    public class researchDAO
    {
        private Entities model = new Entities();

        public IQueryable<research> Get_research_AllData(int res_type)
        {
            if (res_type == 0)
            {
                return from d in model.research
                       where d.state == "1"
                       orderby d.res_datetime descending
                       select d;
            }
            else
            {
                return from d in model.research
                       where d.state == "1" && d.res_type == res_type
                       orderby d.res_datetime descending
                       select d;
            }
        }

        public IQueryable<research> Get_research_AllData(int res_type,int startRowIndex, int maximumRows)
        {
            return Get_research_AllData(res_type).Skip(startRowIndex).Take(maximumRows);
        }

        public int Get_research_AllData_Count(int res_type)
        {
            return Get_research_AllData(res_type).Count();
        }

        public IQueryable<research> Get_research_Data()
        {
            return from d in model.research
                   where d.state == "1"
                   orderby d.res_type, d.res_datetime descending
                   select d;
        }
        public IQueryable<research> Get_research_Data(int startRowIndex, int maximumRows)
        {
            return Get_research_Data().Skip(startRowIndex).Take(maximumRows);
        }
        public int Get_research_DataCount()
        {
            return Get_research_Data().Count();
        }



        public research Get_research(int res_no)
        {
            return (from d in model.research where d.res_no == res_no select d).FirstOrDefault();
        }

        public void Add_research(research d)
        {
            model.research.AddObject(d);
            Update();
        }

        
        public void Delete_research(int res_no, int peo_uid)
        {
            research d = Get_research(res_no);
            d.state = "2";
            d.createuid = peo_uid;
            d.createtime = DateTime.Now;
            Update();
        }

        public void Update()
        {
            model.SaveChanges();
        }
    }
}
